<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 查询手下代理申请
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column prop="levelapplicationform.id" label="申请ID" width="55" align="center"></el-table-column>
                <el-table-column prop="levelapplicationform.nowlevel" label="申请人等级" align="center">
                    <template scope="scope">                    
                    <p v-if="scope.row.levelapplicationform.nowlevel=='1'">一级代理</p>
                    <p v-else-if="scope.row.levelapplicationform.nowlevel=='2'">二级代理</p>
                    <p v-else-if="scope.row.levelapplicationform.nowlevel=='3'">三级代理</p>
                    <p v-else-if="scope.row.levelapplicationform.nowlevel=='4'">普通会员</p>
                    <p v-else-if="scope.row.levelapplicationform.nowlevel>4||scope.row.nowlevel<1">数据有误</p>               
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="申请人姓名" align="center"></el-table-column>
				<el-table-column prop="levelapplicationform.levelapply" label="目标等级" align="center">
                    <template scope="scope">                    
                    <p v-if="scope.row.levelapplicationform.levelapply=='1'">一级代理</p>
                    <p v-else-if="scope.row.levelapplicationform.levelapply=='2'">二级代理</p>
                    <p v-else-if="scope.row.levelapplicationform.levelapply=='3'">三级代理</p>
                    <p v-else-if="scope.row.levelapplicationform.levelapply>3||scope.row.levelapplicationform.levelapply<1">数据有误</p>               
                    </template>
                </el-table-column>
				<el-table-column prop="reName" label="上级姓名" align="center"></el-table-column>
				<el-table-column prop="nReName" label="目标上级姓名" align="center"></el-table-column>
				<el-table-column prop="levelapplicationform.describe" label="申请内容" align="center"></el-table-column>
				<el-table-column prop="levelapplicationform.status" label="当前处理状态" align="center">
                        <template slot-scope="scope">
                        <div v-if="scope.row.levelapplicationform.status=='0'">待处理</div>
                        <div v-if="scope.row.levelapplicationform.status=='1'">已同意</div>
                        <div v-if="scope.row.levelapplicationform.status=='2'">已拒绝</div>
                        </template>
                </el-table-column>
				<el-table-column prop="levelapplicationform.applicationtime" label="申请提交时间" align="center"></el-table-column>
				<el-table-column prop="levelapplicationform.disposetime" label="处理时间" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.levelapplicationform.status=='1'">已同意</div>
                        <div v-if="scope.row.levelapplicationform.status=='2'">已拒绝</div>
                        <div v-if="scope.row.levelapplicationform.status=='0'">
                            <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="agreeApply(scope.row)"
                        >同意</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="refuseApply(scope.row)"
                        >拒绝</el-button>
                        </div>
                        
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="nowform" :model="nowform" label-width="70px">
				<el-form-item label="申请ID">
				    <div v-text="form.id"></div>
				</el-form-item>
                <el-form-item label="目标等级">
                    <el-select v-model="nowform.levelapply" placeholder="请选择">
                        <el-option label="一级代理" value="1"></el-option>
                        <el-option label="二级代理" value="2"></el-option>
                        <el-option label="三级代理" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="申请描述">
                    <el-input type="textarea" rows="5" v-model="nowform.describe"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { selectMyLevelApplicationform,updateLevelApplicationform,deleteLevelApplicationform,selectOtherLevelApplicationform,agreeLevelApplicationform} from '../../api/testIndex.js';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
				agentName:'',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
			form: {
				levelapply: '',
				describe: '',
				id:'',
			},
			nowform:{
				levelapply: '',
				describe: '',
				id:'',
			},
			mid:{
				
			},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.selectOtherLevelApplicationform();
    },
    methods: {
        // 获取后端传送的数据数据
        selectOtherLevelApplicationform() {
            selectOtherLevelApplicationform(this.query).then(res => {
                console.log(res);
				if(res.code==403){
					this.$router.push('/403');
				}
				if(res.code==200){
					this.tableData = res.t.list;
					this.pageTotal = res.t.total;
					console.log(this.tableData)
				}
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        //触发同意操作
        agreeApply(row){
        // 二次确认
            this.$confirm('确定要同意这个申请吗？', '确认', {
                type: 'warning'
            })
                .then(() => {
					this.mid.id=row.levelapplicationform.id;
                    this.mid.newRe=row.levelapplicationform.newReferenceid;
                    this.mid.levelapply=row.levelapplicationform.levelapply;
                    this.mid.newReferenceid=row.levelapplicationform.newreferenceid;
                    this.mid.memberid=row.levelapplicationform.memberid;
                    this.mid.result=1;
					console.log(this.mid);
					agreeLevelApplicationform(this.mid).then(res => {
					    console.log(res);
						if(res=="成功"){
							this.$message.success('已经同意这个申请');
                            this.selectOtherLevelApplicationform();
						}
						if(res=="失败"){
							this.$message.error('操作失败，请联系管理员');
                            this.selectOtherLevelApplicationform();
						}
					});
                })
                .catch(() => {});
        },

        //触发拒绝操作
        refuseApply(row){
        // 二次确认
            this.$confirm('确定要拒绝这个申请吗？', '确认', {
                type: 'warning'
            })
                .then(() => {
					this.mid.id=row.levelapplicationform.id;
                    this.mid.newRe=row.levelapplicationform.newReferenceid;
                    this.mid.levelapply=row.levelapplicationform.levelapply;
                    this.mid.newReferenceid=row.levelapplicationform.newreferenceid;
                    this.mid.result=2;
                    console.log(this.mid);
					agreeLevelApplicationform(this.mid).then(res => {
					    console.log(res);
						if(res=="成功"){
							this.$message.success('已经拒绝这个申请');
                            this.selectOtherLevelApplicationform();
						}
						if(res=="失败"){
							this.$message.error('操作失败，请联系管理员');
                            this.selectOtherLevelApplicationform();
						}
					});
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
			
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
			{
				this.nowform.id=this.form.id,
				updateLevelApplicationform(this.nowform).then(res => {
				    console.log(res);
					if(res=="成功"){
						this.$message.success(`修改第 ${this.idx + 1} 行成功`);
						this.editVisible = false;
						this.$set(this.tableData, this.idx, this.form);
						this.selectMyLevelApplicationform();
					}else{
						this.$message.error(`修改第 ${this.idx + 1} 行失败`)
					}
				});
			}
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.selectMyLevelApplicationform();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
